package com.baseframe.blog.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baseframe.auth.utils.AuthUtil;
import com.baseframe.blog.entity.SnsBlog;
import com.baseframe.blog.entity.request.SnsBlogRequest;
import com.baseframe.blog.entity.vo.SnsBlogVO;
import com.baseframe.blog.service.SnsBlogService;
import com.baseframe.mybatis.utils.BaseMetaObjectUtil;
import com.baseframe.tool.result.R;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/front/blog")
@AllArgsConstructor
public class FrontSnsBlogController {

    private SnsBlogService snsBlogService;


    /**
     * 新增或修改博客
     * @param request
     * @return
     */
    @PostMapping("/save-or-update")
    public R<Object> addBlog(@RequestBody SnsBlogRequest request){

        SnsBlog blog  = new SnsBlog();

        BeanUtils.copyProperties(request,blog, BaseMetaObjectUtil.getUpdateFillPropertiesName(SnsBlog.class));

        blog.setAuthor(AuthUtil.getUserId());

        //重置博文状态
        blog.setOpinion(null);
        blog.setStatus(0);


        return snsBlogService.saveOrUpdate(blog)?R.success():R.fail();
    }

    /**
     * 删除博客
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public R<Object> deleteBlog(@RequestBody SnsBlogRequest request){

        if(request.getId() == null){
            return R.fail("缺少必要请求参数~");
        }
        return snsBlogService.removeById(request.getId())?R.success():R.fail();
    }



    /**
     * 博客详情
     * @param request
     * @return
     */
    @GetMapping("/details")
    public R<Object> details(SnsBlogRequest request){
        SnsBlogVO snsBlogVO = snsBlogService.getDetails(request);

        return snsBlogVO==null?R.fail("博文不存在"):R.data(snsBlogVO);
    }

    @GetMapping("/details/unconditional")
    public R<Object> detailsByUnconditional(SnsBlogRequest request){
        if(request.getId() == null){
            return R.fail("缺少必要请求参数~");
        }

        SnsBlogVO snsBlogVO = snsBlogService.getDetailsByUnconditional(request);

        return R.data(snsBlogVO);
    }


    /**
     * 查询博客
     * @param page
     * @param request
     * @return
     */
    @GetMapping("/search")
    public R<Page<SnsBlogVO>> searchBlog(Page<SnsBlogVO> page, SnsBlogRequest request){


        return R.data(snsBlogService.blogPageListByFront(page,request));
    }

    /**
     * 查看自己的博客
     * @param page
     * @param request
     * @return
     */
    @GetMapping("/page")
    public R<Page<SnsBlogVO>> ownBlogPageList(Page<SnsBlogVO> page, SnsBlogRequest request){


        return R.data(snsBlogService.ownBlogPageList(page,request));
    }

    /**
     * 获取最新的博客内容
     * @return 博客列表
     */
    @GetMapping("/new/list")
    public R<List<SnsBlogVO>> getNewBlogList(){


        return R.data(snsBlogService.getNewList());
    }

    @PostMapping("/change/authority")

    public R<Object> changeAuthority(@RequestBody SnsBlogRequest request){

        if(request.getId()==null){
            return R.fail("缺少必要请求参数");
        }

        if(request.getAuthority()<0||request.getAuthority()>1){
            return R.fail("博文权限参数不合法");
        }

        return snsBlogService.update(new LambdaUpdateWrapper<SnsBlog>().eq(SnsBlog::getId,request.getId()).set(SnsBlog::getAuthority,request.getAuthority()))?R.success():R.fail();
    }


}
